package com.kaiji.mq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Sender {

	private static final Logger __logger = LoggerFactory.getLogger(Sender.class);
	
	public static void main(String[] args) throws JMSException {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageProducer messageProducer = null;
		
		connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, 
				ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
		connection = connectionFactory.createConnection();
		connection.start();
		
		session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
		destination = session.createQueue("kaiji002");
		messageProducer = session.createProducer(destination);
		
		messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		message(session, messageProducer);
		session.commit();
	}

	private static void message(Session session, MessageProducer messageProducer) throws JMSException {
		for(int i = 0; i < 5;  i++) {
			TextMessage message = session.createTextMessage("我发送的消息是数值. 现在是" + i);
			__logger.info("发送消息内容: {}", message.getText());
			messageProducer.send(message);
		}
	}
}
